package com.wanxi.properties;

import com.wanxi.util.Tool;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.util.Properties;
import java.util.Set;

public class WriteStudent {
    public static void main(String[] args) {

        Properties properties = new Properties();
        String path = "E:\\wanxi\\step-2\\java02-0921\\day21OtherIOStream\\src\\com\\wanxi\\properties";
        String fileName = "student.properties";
        String aa = "a.txt";
        InputStream inputStream = Tool.getInputStream(path, fileName);
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Student student = readProperties(properties);
        writeObjectStream(student, path, aa);
    }

    private static void writeObjectStream(Student student, String path, String fileName) {
        ObjectOutputStream outputStream = null;
        try {
            outputStream = new ObjectOutputStream(Tool.getFileOutputStream(path, fileName));
            outputStream.writeObject(student);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static Student readProperties(Properties properties) {
        Set<Object> keys = properties.keySet();
        Student student = null;

        for (Object key : keys
        ) {
//            System.out.println(key + "=" + properties.get(key));
            student = new Student((String) key, Integer.parseInt(properties.get(key).toString()));
        }
        System.out.println(student);
        return student;
    }
}
